﻿#include "CServerThread.h"
#include <QDataStream>

CServerThread::CServerThread()
{
    m_pServer = new CServer;
    connect(m_pServer, SIGNAL(sig_sendMessage(QString)), this, SLOT(slot_sendMessage(QString)));
}

void CServerThread::stop()
{
    if (this->isRunning())
    {
        this->terminate();
    }
}

void CServerThread::run()
{
    m_pServer->listenClient();
}

void CServerThread::slot_sendMessage(QString str)
{
    if ("" == str)
    {
        return;
    }

    //封装包头
    QByteArray dataSend;

    QDataStream stream(&dataSend,QIODevice::WriteOnly);
    stream<<(qint32)0<<str.toLocal8Bit();
    stream.device()->seek(0);
    stream<<dataSend.size();
    m_pServer->m_pClientSocket->write(dataSend);
}
